
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaSecretaria.java
 *
 * Created on 08/09/2010, 11:42:21
 */
package asfabdesk.apresentacao;

import asfabdesk.dominio.Secretaria;
import asfabdesk.presistencia.DAO;
import javax.swing.JOptionPane;

/**
 *
 * @author Marco Aurélio
 */
public class TelaSecretaria extends javax.swing.JInternalFrame {

    private Secretaria secretaria = new Secretaria();

    /** Creates new form TelaSecretaria */
    public TelaSecretaria() {
        initComponents();
    }

    public void salvar() {
        if (verificarCampo()) {
            if (verificarDup()) {
                try {
                    secretaria = povoarSecretaria();
                    new DAO().salvar(secretaria);
                    JOptionPane.showMessageDialog(null, "Cadastro efetuado com Sucesso'.", "Sucesso!", 1);
                    limpar();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Ops, ocorreu um erro durante o cadastro'.", "Ops,erro!", 0);
                }
            }
        }
    }

    public boolean verificarCampo() {
        boolean retorno = true;
        if (txtSecretaria.getText().equals("")) {
            retorno = false;
        }
        if (retorno == false) {
            JOptionPane.showMessageDialog(null, "Ops, você não preencheu o campo secretaria", "Campos Obrigatórios.", 2);
        }
        return retorno;
    }

    public boolean verificarDup() {
        boolean r = true;
        if (new DAO().localizar(Secretaria.class, "SecretariaPorNome", txtSecretaria.getText().toUpperCase()) != null) {
            r = false;
        }
        if (r == false) {
            JOptionPane.showMessageDialog(null, "Ops, você está tentando salvar uma já cadastrada", "Ops.", 2);
        }
        return r;
    }

    public void limpar() {
        txtSecretaria.setText("");
        txtSigla.setText("");
        txtTelefone1.setText("");
        txtTelefone2.setText("");
    }

    public Secretaria povoarSecretaria() {
        Secretaria sec = new Secretaria();
        sec.setDescricao(txtSecretaria.getText().toUpperCase());
        sec.setSigla(txtSigla.getText().toUpperCase());
        sec.setTelefone1(txtTelefone1.getText());
        sec.setTelefone2(txtTelefone2.getText());
        return sec;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        txtSigla = new javax.swing.JTextField();
        btnAdicionar = new javax.swing.JButton();
        txtSecretaria = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtTelefone1 = new javax.swing.JFormattedTextField();
        txtTelefone2 = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setClosable(true);
        setIconifiable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(asfabdesk.apresentacao.AsfabDeskApp.class).getContext().getResourceMap(TelaSecretaria.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel4.setBackground(resourceMap.getColor("jPanel4.background")); // NOI18N
        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtSigla.setFont(resourceMap.getFont("txtSigla.font")); // NOI18N
        txtSigla.setName("txtSigla"); // NOI18N
        jPanel2.add(txtSigla, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 20, 110, 20));

        btnAdicionar.setForeground(resourceMap.getColor("btnAdicionar.foreground")); // NOI18N
        btnAdicionar.setIcon(resourceMap.getIcon("btnAdicionar.icon")); // NOI18N
        btnAdicionar.setText(resourceMap.getString("btnAdicionar.text")); // NOI18N
        btnAdicionar.setMaximumSize(null);
        btnAdicionar.setMinimumSize(null);
        btnAdicionar.setName("btnAdicionar"); // NOI18N
        btnAdicionar.setPreferredSize(null);
        btnAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarActionPerformed(evt);
            }
        });
        jPanel2.add(btnAdicionar, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 70, 110, -1));

        txtSecretaria.setFont(resourceMap.getFont("txtSecretaria.font")); // NOI18N
        txtSecretaria.setMaximumSize(null);
        txtSecretaria.setName("txtSecretaria"); // NOI18N
        jPanel2.add(txtSecretaria, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 230, -1));

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 0, -1, -1));

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, -1, -1));

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 0, -1, -1));

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, 20));

        try {
            txtTelefone1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtTelefone1.setName("txtTelefone1"); // NOI18N
        jPanel2.add(txtTelefone1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, 90, -1));

        try {
            txtTelefone2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtTelefone2.setName("txtTelefone2"); // NOI18N
        jPanel2.add(txtTelefone2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 70, 90, -1));

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, -1, 20));

        jPanel4.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 370, 110));

        getContentPane().add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 390, 130));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarActionPerformed
        salvar();
    }//GEN-LAST:event_btnAdicionarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField txtSecretaria;
    private javax.swing.JTextField txtSigla;
    private javax.swing.JFormattedTextField txtTelefone1;
    private javax.swing.JFormattedTextField txtTelefone2;
    // End of variables declaration//GEN-END:variables
}
